﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Contacts.Maui.Models
{
    public static class ContactRepository
    {
        public static List<Contact> _contactList = new List<Contact>()
        {
           new Contact(){ ContactId=1, Name="11111", Email="11@qq.com"},
           new Contact(){ ContactId=2, Name="222222", Email="22@qq.com"},
           new Contact(){ ContactId=3, Name="333333", Email="33@qq.com"},
        };

        public static List<Contact> GetContacts()
        {
            return _contactList;
        }

        public static Contact GetContactsById(int ContactId)
        {
            var contact = _contactList.FirstOrDefault(a => a.ContactId == ContactId);
            if (contact != null)
            {
                return new Contact
                {
                    ContactId = contact.ContactId,
                    Name = contact.Name,
                    Email = contact.Email,
                    Phone = contact.Phone,
                    Adress = contact.Adress,
                };
            }
            return null;
        }

        public static void UpdateContacts(int ContactId, Contact Contact)
        {
            if (ContactId != Contact.ContactId) return;

            var contactUpdate = _contactList.FirstOrDefault(a => a.ContactId == ContactId);
            if (contactUpdate != null)
            {
                contactUpdate.Name = Contact.Name;
                contactUpdate.Email = Contact.Email;
                contactUpdate.Phone = Contact.Phone;
                contactUpdate.Adress = Contact.Adress;
            }
        }

        public static void AddContacts(Contact Contact)
        {
            int MaxContactId = _contactList.Max(a => a.ContactId);
            int ContactId = MaxContactId + 1;
            _contactList.Add(Contact);
        }

        public static void DeletaContacts(int ContactId)
        {
            var contact = _contactList.FirstOrDefault(a => a.ContactId == ContactId);
            if (contact != null)
            {
                _contactList.Remove(contact);
            }
        }

        public static List<Contact> SearchContacts(string name)
        {
            var contact = _contactList.Where(a => !string.IsNullOrEmpty(a.Name) && a.Name.StartsWith(name, StringComparison.OrdinalIgnoreCase)).ToList();

            if (contact == null || contact.Count <= 0)
                contact = _contactList.Where(a => !string.IsNullOrEmpty(a.Email) && a.Email.StartsWith(name, StringComparison.OrdinalIgnoreCase)).ToList();
            else
                return contact;

            if (contact == null || contact.Count <= 0)
                contact = _contactList.Where(a => !string.IsNullOrEmpty(a.Phone) && a.Phone.StartsWith(name, StringComparison.OrdinalIgnoreCase)).ToList();
            else
                return contact;

            if (contact == null || contact.Count <= 0)
                contact = _contactList.Where(a => !string.IsNullOrEmpty(a.Adress) && a.Adress.StartsWith(name, StringComparison.OrdinalIgnoreCase)).ToList();
            else
                return contact;

            return contact;
        }
    }
}
